﻿using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Qin.Common
{
    /// <summary>
    /// Excel帮助类
    /// </summary>
    public class ExcelHelper
    {
        /// <summary>
        /// 设置表标题
        /// </summary>
        /// <param name="st">表对象</param>
        /// <param name="pis">字段属性信息列表</param>
        /// <param name="titleRow">标题行</param>
        public static void SetSheetTitle(ISheet st, IEnumerable<PropertyInfo> pis, int titleRow = 1)
        {
            if (titleRow <= 0) titleRow = 1;
            var titles = pis.Select(x => x.Name).ToList();
            IRow row = st.CreateRow(titleRow - 1);
            for (int col = 0; col < titles.Count; col++)
                row.CreateCell(col).SetCellValue(titles[col]);
        }

        /// <summary>
        /// 设置表数据
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="st">对象表</param>
        /// <param name="pis">字段属性信息列表</param>
        /// <param name="data">数据</param>
        /// <param name="dataRow">数据其实行</param>
        public static void SetSheetData<T>(ISheet st, IEnumerable<PropertyInfo> pis, IEnumerable<T> data, int dataRow = 2)
            where T : class, new()
        {
            if (dataRow <= 0) dataRow = 1;
            IRow row;
            foreach (var d in data)
            {
                row = st.CreateRow(dataRow - 1);
                int colCount = 0;
                foreach (var pi in pis)
                {
                    row.CreateCell(colCount).SetCellValue(pi.GetValue(d)?.ToString() ?? "");
                    colCount++;
                }
                dataRow++;
            }
        }

        /// <summary>
        /// 是否存在表
        /// </summary>
        /// <param name="wb">工作簿</param>
        /// <param name="sheetName">表名</param>
        /// <returns></returns>
        public static bool IsExistSheet(IWorkbook wb, string sheetName)
            => wb.GetSheet(sheetName) != null;

        /// <summary>
        /// 设置表格数据
        /// </summary>
        /// <typeparam name="T">数据类型</typeparam>
        /// <param name="wb">工作簿</param>
        /// <param name="data">数据</param>
        /// <param name="sheetName">表名(默认为类名)</param>
        /// <returns>对象表<see cref="ISheet"/></returns>
        public static ISheet SetSheet<T>(IWorkbook wb, IEnumerable<T> data, string sheetName = "") where T : class, new()
        {
            var type = typeof(T);
            if (string.IsNullOrEmpty(sheetName))
                sheetName = type.Name;

            var pis = type.GetProperties();
            var s = wb.GetSheet(sheetName);

            if (s != null)
                throw new Exception($"已存在表名{sheetName}");


            ISheet st = wb.CreateSheet(sheetName);
            SetSheetTitle(st, pis);
            SetSheetData(st, pis, data);

            return st;
        }
    }


    /// <summary>
    /// Excel配置特性
    /// </summary>
    public class ExcelAttribute : Attribute
    {
        public ExcelAttribute(string columnName = "", string sheetName = "", bool isIgnore = false)
        {
            ColumnName = columnName;
            SheetName = sheetName;
            IsIgnore = isIgnore;
        }

        /// <summary>
        /// 列名
        /// </summary>
        public string ColumnName { get; set; }
        /// <summary>
        /// 表名
        /// </summary>
        public string SheetName { get; set; }
        /// <summary>
        /// 是否忽略
        /// </summary>
        public bool IsIgnore { get; set; }

    }
}
